4
4
.
.
2
2
.
.
2
2
L
L
a
a
z
z
y
y
C
C
o
o
l
l
u
u
m
m
n
n
F
F
o
o
r
r
I
I
n
n
f
f
o
o
[
[
R
R
]
]
LazyColumnFor is scrollable Container View which organizes his child Views vertically like Column (on top of each other).
You define list of items and Lambda itemContent that is executed for each item (so that you can display them differently).
Syntax
import androidx.compose.foundation.lazy.LazyColumnFor
LazyColumnFor(
items = listOf("A", "B", "C", "D") + ((0..100).map { it.toString() }),
itemContent = { item ->
when (item) {
"A" -> { Text("ALPHA - ") }
"B" -> { Text("BRAVO - ") }
"C" -> { /* Do Nothing */ }
"D" -> { Text("$item - ") }
else -> { Text("$item - ") }
}
}
)
E
E
x
x
a
a
m
m
p
p
l
l
e
e
In this example we create LazyRowFor View with too many Text Views to fit horizontally on the screen.
MainActivity.kt
package com.example.testcompose
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.foundation.Text
import androidx.compose.foundation.lazy.LazyColumnFor
import androidx.compose.ui.platform.setContent
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
LazyColumnFor(
items = listOf("A", "B", "C", "D") + ((0..100).map { it.toString() }),
itemContent = { item ->
when (item) {
"A" -> { Text("ALPHA - ") }
"B" -> { Text("BRAVO - ") }
"C" -> { /* Do Nothing */ }
"D" -> { Text("$item - ") }
else -> { Text("$item - ") }
}
}
)
}
}
}
Output